package com.ruoyi.web.controller.tzzl;

/**
 * @author by caoyong
 * @date 2022/11/7
 **/

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.tzzl.api.RealNameApi;
import com.ruoyi.tzzl.domain.WzCertification;
import com.ruoyi.tzzl.domain.WzProgramUser;
import com.ruoyi.tzzl.domain.WzUserDetails;
import com.ruoyi.tzzl.domain.vo.QueryVo;
import com.ruoyi.tzzl.mapper.WzProgramUserMapper;
import com.ruoyi.tzzl.service.IWzCertificationService;
import com.ruoyi.tzzl.service.IWzProgramUserService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.generic.Type;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 小程序用户Controller
 *
 * @author ruoyi
 * @date 2022-11-07
 */
@RestController
@RequestMapping("/tzzl/user")
public class WzProgramUserController extends BaseController {

    @Autowired
    private IWzProgramUserService wzProgramUserService;

    @Autowired
    private IWzCertificationService wzCertificationService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询小程序用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WzProgramUser wzProgramUser)
    {
        startPage();
        List<WzProgramUser> list = wzProgramUserService.selectWzProgramUserList(wzProgramUser);
        return getDataTable(list);
    }

    /**
     * 查询用户详情
     * */
    @GetMapping("/details")
    public AjaxResult details(WzProgramUser wzProgramUser){
        if (ObjectUtils.isNotEmpty(wzProgramUser.getId())){
            return AjaxResult.success(wzProgramUserService.details(wzProgramUser.getId())) ;
        }else{
           return AjaxResult.success(wzProgramUserService.platform()) ;
        }

    }

    /**修改用户个人资料*/
    @PostMapping("updateDetails")
    public AjaxResult updateDetails(@RequestBody WzUserDetails wzUserDetails){
        return AjaxResult.success(wzProgramUserService.updateDetails(wzUserDetails));
    }

    /**修改用户信息(后台)*/
    @PostMapping("/updateUserInformation")
    public AjaxResult updateUserInformation(@RequestBody WzProgramUser wzProgramUser){
        return AjaxResult.success(wzProgramUserService.updateUserInformation(wzProgramUser));
    }

    /**修改用户自我介绍*/
    @PostMapping("/updateYourself")
    public AjaxResult updateYourself(@RequestBody WzUserDetails wzUserDetails){
        return AjaxResult.success(wzProgramUserService.updateYourself(wzUserDetails));
    }

    /**修改用户支付宝账号*/
    @PostMapping("/updateAlipay")
    public AjaxResult updateAlipay(@RequestBody WzUserDetails wzUserDetails){
        return AjaxResult.success(wzProgramUserService.updateAlipay(wzUserDetails));
    }

    /**
     * 修改用户状态
     * */
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody WzProgramUser wzProgramUser){
      WzProgramUser wzProgramUser1 =  wzProgramUserService.getById(wzProgramUser.getId());
      wzProgramUser1.setStatus(wzProgramUser.getStatus());
        return AjaxResult.success(wzProgramUserService.updateById(wzProgramUser1));
    }

    /**
     * 批量删除用户
     * */
    @DeleteMapping ("/batchDeletion/{ids}")
    public AjaxResult batchDeletion(@PathVariable List<Long> ids){
        return AjaxResult.success(wzProgramUserService.removeByIds(ids));
    }

    /**
     * 用户资料详情
     * */
    @GetMapping("/informationDetails")
    public AjaxResult informationDetails(Long id){
        return AjaxResult.success(wzProgramUserService.informationDetails(id));
    }


    /**
     * 查询用户附近的人
     * */
    @GetMapping("/nearby")
    public AjaxResult nearby(Long id){
        return AjaxResult.success(wzProgramUserService.nearbyList(id));
    }


//    /**
//     * 新增用户
//     * */
//    @PostMapping("/addUser")
//    public AjaxResult addUser(@RequestBody WzProgramUser wzProgramUser){
//        return AjaxResult.success(wzProgramUserService.addUser(wzProgramUser));
//    }

    /**
     * 查询首页展示数据
     * */
    @GetMapping("/homePageData")
    public AjaxResult homePageData(){
        return AjaxResult.success(wzProgramUserService.homePageData());
    }

    /**修改用户在线状态*/
    @PostMapping("/onlineStatus")
    public AjaxResult onlineStatus(@RequestBody WzProgramUser wzProgramUser){
        return AjaxResult.success(wzProgramUserService.onlineStatus(wzProgramUser));
    }

    /**实名认证*/
    @PostMapping("/authentication")
    @RepeatSubmit
    public AjaxResult authentication(@RequestBody WzProgramUser wzProgramUser ){
      String rz =  RealNameApi.authentication(wzProgramUser.getPhone().toString(),wzProgramUser.getUserName());
      if (rz.equals("0")){
          if (wzCertificationService.getIfAuthentication(wzProgramUser.getId()).equals("0")) {
              WzProgramUser wzProgramUser1 = wzProgramUserService.getById(wzProgramUser.getId());
              WzCertification wzCertification =new WzCertification();
              wzCertification.setCreateTime(new Date());//时间
              wzCertification.setWid(wzProgramUser1.getId());
              wzCertification.setCertificationType("1");
              wzCertification.setCertificationState("1"); //认证类型
              wzCertification.setCertificationContent(wzProgramUser.getUserName()+","+wzProgramUser.getPhone()); //认证内容
              wzCertificationService.save(wzCertification);
              wzProgramUser1.setName(wzProgramUser.getUserName());
              wzProgramUser1.setRealNameStatus("1");
              wzProgramUser1.setRealNameTime(new Date()); //实名时间
              wzProgramUserService.updateById(wzProgramUser1);
              return AjaxResult.success(true);
          }else{
              return  AjaxResult.success(false);
          }
      }else{
          return AjaxResult.success(false);
      }
    }

    /**查看用户消息记录*/
    @GetMapping("/informationRecord")
    public AjaxResult informationRecord(Long id){
        return AjaxResult.success(wzProgramUserService.informationRecord(id));
    }

    /**查看我的团队用户列表*/
    @GetMapping("/myTeam")
    public AjaxResult myTeam(Long id, String type){
        return AjaxResult.success(wzProgramUserService.myTeam(id,type));
    }

        /**查看团队营收*/
    @GetMapping("/teamReceivable")
    public AjaxResult teamReceivable(Long id){
        return AjaxResult.success(wzProgramUserService.teamReceivable(id));
    }

    /**查看团队邀请记录*/
    @GetMapping("/invitationRecord")
    public AjaxResult invitationRecord(Long id,String state){
        return AjaxResult.success(wzProgramUserService.invitationRecord(id,state));
    }

    /**免费开通会员*/
    @PostMapping("/freeMembership")
    public AjaxResult freeMembership(@RequestBody WzProgramUser wzProgramUser){
        return AjaxResult.success(wzProgramUserService.freeMembership(wzProgramUser));
    }

    /**定时更新用户状态*/
    @PostMapping("/timingToUpdate")
    public AjaxResult timingToUpdate(@RequestBody WzProgramUser wzProgramUser){
        WzProgramUser user =wzProgramUserService.getById(wzProgramUser.getId());
        user.setOnlineStatus("1"); //在线状态
        user.setUpdateTime(new Date());
        String key = "ZX"+user.getId();
        redisCache.setCacheObject(key,key,1200, TimeUnit.SECONDS); //过期时间为20分钟 单位秒
        return AjaxResult.success(wzProgramUserService.updateById(user));
    }


}
